<?php
namespace Admin\Controller;
use Think\Controller;
class NoBaseController extends Controller {
    public function login(){
        $this->display();
    }
    public function doLogin(){
    	$name=I('post.username');
    	$psd=I('post.userpsd');
    	$dis=substr(md5(substr($psd,0,2)),0,2);
    	$psd.=$dis;
    	$result=$this->checkpsd($name,$psd);
    	if($result){
    		$this->redirect('Index/index');
    	}else{
    		$this->error('账号或密码错误');
    	}
    	
    }
    public function checkpsd($name,$psd){
        $code=0;
        $map=array(
        	'user_status'=>1,
        	'user_name'=>$name
        );
        $data=M('User')->where($map)->find();
        if($data['user_password']===md5($psd)){
        	session('uid',$data['user_id']);
        	session('username',$name);
          $add=array(
              'user_id'=>$data['user_id'],
              'log_action'=>'login',
              'log_createtime'=>time(),
              'log_ip'=>get_client_ip()
          );
          add($add,M('log'));
          $code=1;  
        }
        return $code;
    }
    //退出登录
    function outlogin(){
       session('uid',false);
       session('username',false);
       echo session('username');
       $this->error('退出登录','login');
    }
    function viewdata(){
      $url=I('nowurl');
      $rurl=I('rUrl');
      if (empty($url)){$this->error('无参数');}
      $ip = get_client_ip();
      $Ip = new \Org\Net\IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
      $area = $Ip->getlocation($ip); // 获取某个IP地址所在的位置
      
      $data=array(
         'v_url'=>$url,
         'v_ip'=>$ip,
         'v_createtime'=>time(),
         'v_userid'=>session('uid'),
         'v_address'=>$area['country'].$area['area'],
      );
      

      M('View')->add($data);
      $mip=M('Viewip');
      $where=array('ip_ip'=>$ip);
      $count=$mip->where($where)->count();
      $dataip=array(
        'ip_ip'=>$ip,
        'ip_createtime'=>time(),
        'ip_way'=>'0'
      );
      if(!empty($rurl)){
            $data['v_rurl']=$rurl;
            if(strops($rurl,'www.baidu.com')!==false){
                $dataip['ip_way']="engine";
                $dataip['ip_engine']='baidu';
            }else if(strops($rurl,'www.sogou.com')!==false){
                $dataip['ip_way']="engine";
                $dataip['ip_engine']='sogou';
            }else if(strops($rurl,'www.so.com')!==false){
                $dataip['ip_way']="engine";
                $dataip['ip_engine']='so';
            }else if(strops($rurl,'www.yahoo.com')!==false){
                $dataip['ip_way']="engine";
                $dataip['ip_engine']='yahoo';
            }else{
                $dataip['ip_way']=1;
                $dataip['ip_engine']=0;
            }
      }
      if($count) {
        $mip->where($where)->save($dataip);
      }else{
        $mip->add($dataip);
      }
      exit();
    }

    function shiyan(){
       shiyan();
    }
    public function uploads(){
        return 'dsrherj';
        $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    './Uploads/',
            'savePath'   =>    '',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        // 上传文件 
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            return array('status'=>0,'msg'=>$upload->getError());
            //$this->error($upload->getError());
        }else{// 上传成功
            return array('status'=>1,'msg'=>$info);
        }
    }
}